package com.lh0811.forge.pserv.basic.server.adapter.controller;

import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.pserv.basic.api.param.*;
import com.lh0811.forge.pserv.basic.api.vo.CommDictQueryVo;
import com.lh0811.forge.pserv.basic.api.vo.CommDictTypeVo;
import com.lh0811.forge.pserv.basic.server.service.CommDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "基础服务-字典管理")
@RestController
@RequestMapping("/forge/plate/v1/basic/dict")
public class DictController {

    @Resource
    private CommDictService commDictService;

    @GetMapping("/type/query/page")
    @Operation(summary = "查询字典类型列表(分页)", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<PageInfo<CommDictTypeVo>> dictTypeQueryPage(@ParameterObject CommDictTypeQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess(commDictService.dictTypeQueryPage(param));
    }

    @GetMapping("/type/query/list")
    @Operation(summary = "查询字典类型列表(不分页)", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<CommDictTypeVo>> dictTypeQueryList(@ParameterObject CommDictTypeQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess(commDictService.dictTypeQueryList(param));
    }

    @PutMapping("/type/create")
    @Operation(summary = "创建字典类型", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictTypeCreate(@RequestBody CommDictTypeCreateParam param) throws Exception {
        commDictService.dictTypeCreate(param);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/type/update")
    @Operation(summary = "修改字典类型", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictTypeUpdate(@RequestBody CommDictTypeUpdateParam param) throws Exception {
        commDictService.dictTypeUpdate(param);
        return ServerResponse.createBySuccess();
    }

    @DeleteMapping("/type/delete/{dictTypeId}")
    @Operation(summary = "删除字典类型", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictTypeDelete(@PathVariable(value = "dictTypeId") Long dictTypeId) throws Exception {
        commDictService.dictTypeDelete(dictTypeId);
        return ServerResponse.createBySuccess();
    }


    @PutMapping("/create")
    @Operation(summary = "创建字典", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictCreate(@RequestBody CommDictCreateParam param) throws Exception {
        commDictService.dictCreate(param);
        return ServerResponse.createBySuccess();
    }

    @DeleteMapping("/delete/{dictId}")
    @Operation(summary = "删除字典", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictDelete(@PathVariable(value = "dictId") Long dictId) throws Exception {
        commDictService.dictDelete(dictId);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/update")
    @Operation(summary = "字典更新", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse dictUpdate(@RequestBody CommDictUpdateParam param) throws Exception {
        commDictService.dictUpdate(param);
        return ServerResponse.createBySuccess();
    }

    @GetMapping("/query/page")
    @Operation(summary = "查询字典信息列表(分页)", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<PageInfo<CommDictQueryVo>> dictQueryPage(@ParameterObject CommDictQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess(commDictService.dictQueryPage(param));
    }

    @GetMapping("/query/list")
    @Operation(summary = "查询字典信息列表(不分页)", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<CommDictQueryVo>> dictQueryList(@ParameterObject CommDictQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess(commDictService.dictQueryList(param));
    }

}
